/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#ifndef SQLITEDB_H_
#define SQLITEDB_H_

#include <QString>
#include <QDir>
#include <QMutexLocker>
#include <QMutex>

#include <iostream>
#include <map>
#include <string>
#include <sqlite3.h>

#include "StaticModule.h"

using namespace std;

enum QueryType
{
	//write only
	QueryT_Write,

	//read only
	QueryT_Read,

	QueryT_Last
};

#define DB INSTANCE(SQLiteDB)

class SQLiteDB
{
public:
	SQLiteDB();
	virtual ~SQLiteDB();

	bool openDB(QString fileName, QString dbName, bool create = false);
	void closeDB(QString dbName);
	bool hasDB(QString dbName);

	void createEmptyTable(QString dbName, QString tableName, QStringList colNames);

	bool execQuery(QString command, QueryType queryType=QueryT_Last, QString dbName = QString()); //return true if query successfully

	bool quickUpdate(QString command){return execQuery(command, QueryT_Write, curUsr);}

	int updateBlob(QString command, QByteArray buffer, QString dbName);//use '?' to replace binary content
	int quickUpdateBlob(QString command, QByteArray buffer){return updateBlob(command, buffer, curUsr);}

	bool quickGet(QString command){return execQuery(command, QueryT_Read, curUsr);}

	QByteArray getSingleBlob(QString command, QString dbName);
	QByteArray quickGetSingleBlob(QString command){return getSingleBlob(command, curUsr);}

	int getRowCount(){return mRowCount;}
	int getColCount(){return mColumnCount;}
	QString getColName(int col);
	QString getTableValue(int row, int column);
	QString getTableValue(int row, QString columnName);

	QString getErrMsg(){return QString(mErrMsg);}

	QString curUsr;

private:
	map<QString, sqlite3*> mDBs;

	char* mErrMsg;
	char** mDBResult;
	bool mDBResultValid;
	int mRowCount;
	int mColumnCount;
	int mQueryResult;
	QueryType mLastQueryType;
	QString mLastQueryDB;

	QMutex mQMutex;
};

#endif /* SQLITEDB_H_ */
